package com.wh.testCAS;

import java.util.concurrent.atomic.AtomicInteger;

//CAS: compareAndSet 比较并交换
public class CASDemo {
    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(2021);
        //                                          期望，更新
        //public final boolean compareAndSet(int expect, int update)
        //如果我期望的值达到了，那么就更新，否则，就不更新
        boolean b = atomicInteger.compareAndSet(2021, 2022);
        System.out.println(b);
        System.out.println(atomicInteger.get());

        boolean b2 = atomicInteger.compareAndSet(2021, 2022);
        System.out.println(b2);
        System.out.println(atomicInteger.get());
        atomicInteger.getAndIncrement();

    }
}
